﻿using System;
using System.Drawing.Imaging;
using System.IO;
using System.Windows.Forms;
using PlaylistEditorForCentrafuse.Playlist;

namespace PlaylistEditorForCentrafuse.GUI
{
    public partial class frmAudioFileDetails : Form
    {
        #region Private Members
        private bool _imageChanged = false;
        #endregion

        #region Public Members
        public PlaylistAudioFile Audiofile { get; set; }
        #endregion

        #region Constructor
        public frmAudioFileDetails()
        {
            InitializeComponent();
        }
        #endregion

        #region Event Handler
        private void frmAudioFileDetails_Shown(object sender, EventArgs e)
        {
            // Alles zurücksetzen
            ClearTabGeneral();
            ClearTabID3v1();
            ClearTabID3v2();
            ClearTabCover();
            ClearTabUnsyncedLyrics();
            _imageChanged = false;

            if (Audiofile != null)
            {
                // Tab Allgemein
                txtFilename.Text = Audiofile.FileName;
                lstPlaylists.Items.Clear();

                foreach (PlaylistFile prop in Audiofile.Playlists)
                {
                    lstPlaylists.Items.Add(prop.Filename);
                }
                
                if (Audiofile.ID3v1 != null)
                {
                    // Tab ID3v1
                    txtV1Album.Text = Audiofile.ID3v1.Album;
                    txtV1Artist.Text = Audiofile.ID3v1.Artist;
                    txtV1Comments.Text = Audiofile.ID3v1.Comment;
                    txtV1Genre.Text = Audiofile.ID3v1.Genre;
                    txtV1Title.Text = Audiofile.ID3v1.Song;
                    txtV1Track.Text = Audiofile.ID3v1.Track;
                    txtV1Version.Text = "1.1";
                    txtV1Year.Text = Audiofile.ID3v1.Year;
                }
                
                if (Audiofile.ID3v2 != null)
                {
                    // Tab ID3v2
                    txtV2Album.Text = Audiofile.ID3v2.Album;
                    txtV2Artist.Text = Audiofile.ID3v2.Artist;
                    txtV2Genre.Text = Audiofile.ID3v2.Genre;
                    txtV2Title.Text = Audiofile.ID3v2.Title;
                    txtV2Track.Text = Audiofile.ID3v2.Track;
                    txtV2Version.Text = "2." + Audiofile.ID3v2.FrameModel.Header.Version.ToString() +
                                        "." + Audiofile.ID3v2.FrameModel.Header.Revision.ToString();
                    txtV2Year.Text = Audiofile.ID3v2.Year;
                    txtV2Comments.Text = Audiofile.ID3v2.Comment;
                    picCover.Image = Audiofile.ID3v2.Picture;
                    txtLyrics.Text = Audiofile.ID3v2.Lyrics;
                }// if (Audiofile.TagHandler != null)
            }// if (Audiofile != null)
        }

        private void picCoverMain_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            ExtractCover();
        }

        private void contextMenuCover_Opening(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (Audiofile != null && Audiofile.ID3v2 != null && Audiofile.ID3v2.Picture != null)
            {
                cMenuCoverExtract.Enabled = true;
            }
            else
            {
                cMenuCoverExtract.Enabled = false;
            }
        }

        private void cMenuCoverExtract_Click(object sender, EventArgs e)
        {
            ExtractCover();
        }

        private void frmAudioFileDetails_FormClosing(object sender, FormClosingEventArgs e)
        {
            // Tags geändert ?
            if (IsTaginfoChanged())
            {
                DialogResult res = MessageBox.Show("Die Taginformationen wurden verändert.\nÄnderungen speichern?",
                                                   "Veränderte Taginformationen",
                                                   MessageBoxButtons.YesNoCancel,
                                                   MessageBoxIcon.Question);
                if (res == DialogResult.Cancel)
                {
                    // Nicht schließen
                    e.Cancel = true;
                }
                else if (res == DialogResult.Yes)
                {
                    SaveChanges();
                }
            }
        }
        #endregion

        #region Private Methods
        private void ClearTabGeneral()
        {
            txtFilename.Text = "";
            lstPlaylists.Items.Clear();
        }

        private void ClearTabID3v1()
        {
            txtV1Album.Text = "";
            txtV1Artist.Text = "";
            txtV1Comments.Text = "";
            txtV1Genre.Text = "";
            txtV1Title.Text = "";
            txtV1Track.Text = "";
            txtV1Version.Text = "";
            txtV1Year.Text = "";
        }

        private void ClearTabID3v2()
        {
            txtV2Album.Text = "";
            txtV2Artist.Text = "";
            txtV2Genre.Text = "";
            txtV2Title.Text = "";
            txtV2Track.Text = "";
            txtV2Version.Text = "";
            txtV2Year.Text = "";
            txtV2Comments.Text = "";
        }

        private void ClearTabCover()
        {
            picCover.Image = null;
        }

        private void ClearTabUnsyncedLyrics()
        {
            txtLyrics.Text = "";
        }

        private void ExtractCover()
        {
            if (Audiofile != null)
            {
                if (Audiofile.ID3v2 != null && Audiofile.ID3v2.Picture != null)
                {
                    
                    SaveFileDialog dlg = new SaveFileDialog();
                    dlg.Filter = "Bitmap (*.bmp)|*.bmp|Extended Metafile (*.emf)|*.emf|" +
                                "Graphics Interchange Format (*.gif)|*.gif|" + 
                                "Joint Photographic Experts Group (*.jpg)|*.jpg|" +
                                "Portable Network Graphics (*.png)|*.png|" +
                                "Tagged Image File Format (*.tiff)|*.tiff|" + 
                                "Windows Metafile (*.wmf)|*.wmf";
                    dlg.OverwritePrompt = true;
                    dlg.FileName = Path.GetFileName(Audiofile.ID3v2.Album);
                    
                    // Speichern
                    if (dlg.ShowDialog() == DialogResult.OK)
                    {
                        // Format
                        ImageFormat format = ImageFormat.Jpeg;
                        switch (Path.GetExtension(dlg.FileName))
                        {
                            case ".bmp":
                                format = ImageFormat.Bmp;
                                break;
                            case ".emf":
                                format = ImageFormat.Emf;
                                break;
                            case ".gif":
                                format = ImageFormat.Gif;
                                break;
                            case ".png":
                                format = ImageFormat.Png;
                                break;
                            case ".tiff":
                                format = ImageFormat.Tiff;
                                break;
                            case ".wmf":
                                format = ImageFormat.Wmf;
                                break;
                        }

                        Audiofile.ID3v2.Picture.Save(dlg.FileName, format);
                    }// if (dlg.ShowDialog() == DialogResult.OK)
                }// if (Audiofile.TagHandler != null)
            }//  if (Audiofile != null)
        }

        private void SaveChanges()
        {
            if (IsTaginfoChanged())
            {
                // Änderungen übernehmen
                // ID3v2
                if (Audiofile.ID3v2 != null)
                {
                    Audiofile.ID3v2.Album = txtV2Album.Text;
                    Audiofile.ID3v2.Artist = txtV2Artist.Text;
                    Audiofile.ID3v2.Comment = txtV2Comments.Text;
                    Audiofile.ID3v2.Genre = txtV2Genre.Text;
                    Audiofile.ID3v2.Lyrics = txtLyrics.Text;
                    Audiofile.ID3v2.Title = txtV2Title.Text;
                    Audiofile.ID3v2.Track = txtV2Track.Text;
                    Audiofile.ID3v2.Year = txtV2Year.Text;
                    Audiofile.ID3v2.Picture = picCover.Image;
                }

                // ID3v1
                if (Audiofile.ID3v1 != null)
                {
                    Audiofile.ID3v1.Album = txtV1Album.Text;
                    Audiofile.ID3v1.Artist = txtV1Artist.Text;
                    Audiofile.ID3v1.Comment = txtV1Comments.Text;
                    Audiofile.ID3v1.Genre = txtV1Genre.Text;
                    Audiofile.ID3v1.Song = txtV1Title.Text;
                    Audiofile.ID3v1.Track = txtV1Track.Text;
                    Audiofile.ID3v1.Year = txtV1Year.Text;
                }

                // Tag speichern
                Audiofile.WriteTags();
            }
        }

        private bool IsTaginfoChanged()
        {
            // ID3v2 überprüfen
            if (Audiofile.ID3v2 != null)
            {
                if (Audiofile.ID3v2.Album   != txtV2Album.Text    ||
                    Audiofile.ID3v2.Artist  != txtV2Artist.Text   ||
                    Audiofile.ID3v2.Comment != txtV2Comments.Text ||
                    Audiofile.ID3v2.Genre   != txtV2Genre.Text    ||
                    Audiofile.ID3v2.Lyrics  != txtLyrics.Text     ||
                    Audiofile.ID3v2.Title   != txtV2Title.Text    ||
                    Audiofile.ID3v2.Track   != txtV2Track.Text    ||
                    Audiofile.ID3v2.Year    != txtV2Year.Text     ||
                    _imageChanged)
                {
                    return true;
                }
            }

            // ID3v1 überprüfen
            if (Audiofile.ID3v1 != null)
            {
                if (Audiofile.ID3v1.Album   != txtV1Album.Text    ||
                    Audiofile.ID3v1.Artist  != txtV1Artist.Text   ||
                    Audiofile.ID3v1.Comment != txtV1Comments.Text ||
                    Audiofile.ID3v1.Genre   != txtV1Genre.Text    ||
                    Audiofile.ID3v1.Song    != txtV1Title.Text    ||
                    Audiofile.ID3v1.Track   != txtV1Track.Text    ||
                    Audiofile.ID3v1.Year    != txtV1Year.Text)
                {
                    return true;
                }
            }

            return false;
        }
        #endregion
    }
}
